/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package weka.attributeSelection.semiAS.semiClusterAS;

import weka.core.Instances;

/**
 *
 * @author Administrator
 */
public class FisherFeatClassEval extends FeatClassEval {

    private Instances[] m_data;

    public FisherFeatClassEval(final Instances data, final Instances[] classData) {
        super(data);
        m_data = classData;
    }

    public double getScore(int i) {
        double vt = calVariance(m_labeledInstances, i);
        double vr = 0.0;
        for (int c = 0; c < m_numClasses; c++) {
            vr = vr + calVariance(m_data[c], i);
        }
        vr = vr / m_numClasses;

        return vt / vr;
    }

    private double calVariance(Instances inst, int i) {
        double v = 0.0;
        int n = inst.numInstances();
        if (n == 0) {
            return 0.0;
        }
        if (inst.attribute(i).isNumeric()) {
            v = inst.variance(i);
        } else {
            double ave = inst.meanOrMode(i);
            for (int j = 0; j < n; j++) {
                double diff = 0.0;
                if (inst.instance(j).value(i) != ave) {
                    diff = 1;
                }
                v = v + diff * diff;
            }
        }
        return v;
    }
}
